{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f4188b41-0bb5-4035-8c5a-56810aa7a90e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from IPython import display\n",
    "from d2l import torch as d2l\n",
    "train_iter,test_iter = d2l.load_data_fashion_mnist(batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6f8eaff4-3298-4625-afc4-612121cdc5f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_10580\\2279473492.py:8: UserWarning: nn.init.normal is now deprecated in favor of nn.init.normal_.\n",
      "  nn.init.normal(m.weight,std=0.01)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Sequential(\n",
       "  (0): Flatten(start_dim=1, end_dim=-1)\n",
       "  (1): Linear(in_features=784, out_features=10, bias=True)\n",
       ")"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#初始化模型参数\n",
    "from torch import nn\n",
    "batch_size = 256\n",
    "\n",
    "net = nn.Sequential(nn.Flatten(),nn.Linear(784,10))\n",
    "def init_weights(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.normal(m.weight,std=0.01)\n",
    "net.apply(init_weights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "447f4ecb-cb7c-4c49-aef7-5970c4bdd174",
   "metadata": {},
   "outputs": [],
   "source": [
    "# loss\n",
    "loss = nn.CrossEntropyLoss(reduction='none')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d32b38c0-8c6f-425f-96ce-4cedee26c64f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 优化器\n",
    "trainer = torch.optim.SGD(net.parameters(),lr=0.1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
